package com.ymx.darling.type;

import com.ymx.darling.util.EnumUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 枚举处理
 */
public class EnumTypeHandler implements TypeHandler<Enum<?>> {
    private final Class<Enum<?>> enumType;

    public EnumTypeHandler(Class<Enum<?>> type) {
        this.enumType = type;
    }

    @Override
    public Enum<?> getResult(ResultSet resultSet, int columIndex) throws SQLException {
        Object enumValue = resultSet.getObject(columIndex);
        return EnumUtil.valueOf(enumType, enumValue);
    }

    @Override
    public void setParameter(PreparedStatement statement, Enum<?> value, int index) throws SQLException {
        statement.setObject(index, EnumUtil.getValue(value));
    }
}
